package com.kaifamiao.exercises.methods;

public class Chronology {
    // private 表示私有的
    private static final char[] HEAVENLY_STEMS ; // 天干
    // static 表示与类相关的、属于类的
    private static final char[] EARTHLY_BRANCHES ; // 地支
    // final 表示最终的、不可更改的
    private static final char[] CHINESE_ZODIAC ; // 生肖

    // 因为 1984年恰好是 甲子 鼠年 因此以它为参照年份
    private static final int CONTRAST = 1984 ; // 参照年份

    static {
        HEAVENLY_STEMS = new char[]{ '甲' , '乙' , '丙' , '丁' , '戊' , '己' , '庚' , '辛' , '壬' , '癸' };
        EARTHLY_BRANCHES = new char[]{ '子' , '丑' , '寅' , '卯' , '辰' , '巳' ,  '午' , '未' , '申' , '酉' , '戌' , '亥' };
        CHINESE_ZODIAC = new char[]{ '鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪' } ;
    }

    public static void show(){
        for (int i = 0; i < 60; i++) {
            int hsIndex = i % HEAVENLY_STEMS.length;
            char hs = HEAVENLY_STEMS[hsIndex];

            int ebIndex = i % EARTHLY_BRANCHES.length;
            char eb = EARTHLY_BRANCHES[ebIndex];

            String he = hs + "" + eb;
            System.out.printf( ( i + 1 ) % 12 == 0 ? "%s\n" : "%s\t", he );
        }
    }

    public static String seek( int year ) {
        // 用给定年份减去参照年份求差值
        int x = year - CONTRAST;
        System.out.println( "年份之差: " + x );
        int y = x % 60;
        System.out.println( "模60后的余数: " + y );
        if( y < 0 ){
            y = y + 60;
        }
        System.out.println( "60以内的非负整数: " + y );

        int hsIndex = y % HEAVENLY_STEMS.length;
        char hs = HEAVENLY_STEMS[hsIndex];

        int ebIndex = y % EARTHLY_BRANCHES.length;
        char eb = EARTHLY_BRANCHES[ebIndex];

        String he = hs + "" + eb;

        return he;
    }

    public static void main(String[] args) {
        Chronology.show();

        System.out.println( Chronology.seek( 2045 ) );
        System.out.println( Chronology.seek( 1923 ) );
    }

}
